<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="排序号" prop="sortNo">
						<el-input-number v-model="form.sortNo" placeholder="排序号" clearable></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="物料编号" prop="materialCode">
						<el-input v-model="form.materialCode" placeholder="物料编号" maxlength="40" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="产品名称" prop="name">
						<el-input v-model="form.name" placeholder="产品名称" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="所属分类" prop="sortId">
						<el-cascader v-model="form.sortId" :options="productSort" :props="productSortProps" :show-all-levels="false" clearable></el-cascader>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="品牌" prop="brand">
						<el-input v-model="form.brand" placeholder="品牌" maxlength="40" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="产品型号" prop="model">
						<el-input v-model="form.model" placeholder="产品型号" maxlength="40" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="价格" prop="price">
						<el-input-number v-model="form.price" placeholder="价格" clearable></el-input-number>/元
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="指导价" prop="guidePrice">
						<el-input-number v-model="form.guidePrice" placeholder="指导价" clearable></el-input-number>/元
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注说明">
				<el-input v-model="form.remark" type="textarea" placeholder="备注说明" maxlength="180" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
export default {
	emits: ['success', 'closed'],
	components: {
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '产品录入对话框',
				edit: '编辑产品对话框',
			},
			productSort: [],
			productSortProps: {
				value: "sortId",
				label: "sortName",
				emitPath: false,
				checkStrictly: true
			},
			form: {
				productId: '',
				sortNo: 0,
				materialCode: '',
				name: '',
				sortId: '',
				brand:'',
				model:'',
				price:0,
				guidePrice:0,
				remark: '',
			},
			options: [{
				value: '0',
				label: '请选择'
			}],
			rules: {
				materialCode: [
					{required: true, message: '产品编号不能为空', trigger: 'blur'}
				],
				sortId: [
					{required: true, message: '产品分类不能为空', trigger: 'blur'}
				],
				name: [
					{required: true, message: '产品名称不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getCrmProductSortTree();
	},
	methods: {
		async getCrmProductSortTree() {
			var res = await this.$API.crm.crmProductSort.getCrmProductSortTree.get();
			this.productSort = res.data
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.crm.crmProduct.insertCrmProduct.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.crm.crmProduct.updateCrmProduct.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.productId = data.productId
			this.form.sortNo = data.sortNo
			this.form.name = data.name
			this.form.materialCode = data.materialCode
			this.form.sortId = data.sortId
			this.form.brand = data.brand
			this.form.price = data.price
			this.form.model = data.model
			this.form.guidePrice = data.guidePrice
			this.form.remark = data.remark
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
